package com.maxjay.system.web.tag;

import java.io.Writer;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.lang.StringUtils;

import com.maxjay.common.exception.ERR;
import com.maxjay.common.exception.GeneException;
import com.maxjay.system.core.cache.BufferedDictTableUtils;
import common.Logger;

public class CodeConvertTag extends BodyTagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private static Logger logger = Logger.getLogger(CodeConvertTag.class);
	private String codeTable;
	private String codeValue = null;

	@Override
	public int doStartTag() throws JspException {
		try {
			Writer writer = this.pageContext.getOut();
			if (StringUtils.isNotBlank(codeTable)) {
				String codeName = BufferedDictTableUtils.getName(codeTable, codeValue);
				writer.write(codeName);
			} else {
				throw GeneException.getGeneException(ERR.ERROR_ERR_MSG, "page:codeConvert标签所需参数codeTable的值不能为空");
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw GeneException.getGeneException(ERR.ERROR_ERR_MSG, e);
		}
		return BodyTagSupport.EVAL_BODY_INCLUDE;
	}

	public String getCodeTable() {
		return codeTable;
	}

	public void setCodeTable(String codeTable) {
		this.codeTable = codeTable;
	}

	public String getCodeValue() {
		return codeValue;
	}

	public void setCodeValue(String codeValue) {
		this.codeValue = codeValue;
	}

}